﻿Public Class frmMTipopreguntaws
    Public arrConsulta(2) As String
    Private arrCampos() As String = {"preguntatipo_id", "preguntatipo_titulo", "rolespecialista_id", "rolespecialista_nombre"}

    Private Sub frmMTipopreguntaws_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        mdiPrincipal.tsmiTipoPreguntasws.Enabled = True
    End Sub

    Private Sub frmMTipopreguntaws_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        refreshList()
    End Sub

    Private Sub tsbNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbNuevo.Click
        Me.tsbNuevo.Enabled = False
        Me.renderForm("Nuevo - Tipo Pregunta", 1)
    End Sub

    Private Sub tsbConsultar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbConsultar.Click
        Me.tsbConsultar.Enabled = False
        Me.renderForm("Consultar - Tipo Pregunta", 2)
    End Sub

    Private Sub tsbEditar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbEditar.Click
        Dim strId As String = Me.dgvTipopregunta.Rows(Me.dgvTipopregunta.CurrentRow.Index).Cells(0).Value.ToString()

        If (strId <> "") Then
            Me.tsbEditar.Enabled = False
            renderForm("Editar - Tipo Pregunta", 3)
        Else
            MsgBox("Debe seleccionar un registro", MsgBoxStyle.Information, "Tipo Pregunta")
        End If
    End Sub

    Private Sub tsbBorrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbBorrar.Click
        Dim objResult = MsgBox("Esta seguro que desea borrar el registro seleccionado?", MsgBoxStyle.YesNo, "Borrar - Tipo Pregunta")

        If (objResult = DialogResult.Yes) Then
            Dim strId As String = Me.dgvTipopregunta.Rows(Me.dgvTipopregunta.CurrentRow.Index).Cells(0).Value.ToString

            mdlConexion.blnOnDB = True
            mdlCRUD.delRecord("preguntatipo", "preguntatipo_id = '" & strId & "'")
            mdlConexion.blnOnDB = False

            If (mdlCRUD.blnFlag = True) Then
                Me.refreshList()
            End If
        End If
    End Sub

    Private Sub tsbRecargar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbRecargar.Click
        arrConsulta(0) = ""
        arrConsulta(1) = ""
        arrConsulta(2) = ""
        Me.refreshList()
    End Sub

    Private Sub renderForm(ByVal strTitle As String, ByVal intMode As Integer)
        Dim frmTipopregunta As New frmTipopreguntaws

        frmTipopregunta.MdiParent = mdiPrincipal
        frmTipopregunta.frmParent = Me

        Dim dstRecord As New DataSet
        dstRecord.Tables.Add("Rolespecialista")

        mdlConexion.blnOnDB = True
        mdlCRUD.loadRecord(dstRecord, "Rolespecialista", "rolespecialista", "", "rolespecialista_nombre")
        mdlConexion.blnOnDB = False

        frmTipopregunta.cmbRolespecialista.DataSource = dstRecord.Tables("Rolespecialista")
        frmTipopregunta.cmbRolespecialista.DisplayMember = dstRecord.Tables("Rolespecialista").Columns(2).Caption.ToString
        frmTipopregunta.cmbRolespecialista.ValueMember = dstRecord.Tables("Rolespecialista").Columns(0).Caption.ToString

        frmTipopregunta.modeForm = intMode
        frmTipopregunta.Text = strTitle
        frmTipopregunta.Show()

    End Sub

    Public Sub refreshList()
        Dim dstRecord As New DataSet
        Dim strCondicion As String = ""
        dstRecord.Tables.Add("Tipopregunta")

        For i As Integer = 0 To 2
            If (Me.arrConsulta(i) <> "") Then
                strCondicion &= IIf(strCondicion <> "", " AND ", "") & IIf(i = 2, Me.arrCampos(i) & " = '" & LCase(Me.arrConsulta(i)) & "'", "LOWER(" & Me.arrCampos(i) & "::text) LIKE '%" & LCase(Me.arrConsulta(i)) & "%'")
            End If
        Next

        mdlConexion.blnOnDB = True
        mdlCRUD.loadRecord(dstRecord, "Tipopregunta", "vistapreguntatipo", strCondicion, "preguntatipo_id")
        mdlConexion.blnOnDB = False

        Me.dgvTipopregunta.DataSource = dstRecord.Tables("Tipopregunta")

        Me.dgvTipopregunta.Columns(2).Visible = False
        Me.dgvTipopregunta.Columns(0).HeaderText = "Codigo"
        Me.dgvTipopregunta.Columns(1).HeaderText = "Tipo Pregunta"
        Me.dgvTipopregunta.Columns(3).HeaderText = "Rol Especialista"
        Me.dgvTipopregunta.Columns(0).Width = 60
        Me.dgvTipopregunta.Columns(1).Width = 260
        Me.dgvTipopregunta.Columns(3).Width = 260
    End Sub
End Class